<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Module System
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial032.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial034.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc67">4.10</A>&nbsp;&nbsp;Module System</H2><UL>
<LI><A HREF="tutorial033.html#toc36">Overview</A>
<LI><A HREF="tutorial033.html#toc37">Making a Module</A>
<LI><A HREF="tutorial033.html#toc38">Using a Module</A>
<LI><A HREF="tutorial033.html#toc39">Qualified Goals</A>
<LI><A HREF="tutorial033.html#toc40">Exporting items other than Predicates</A>
</UL>

<A NAME="secmodules"></A>
<A NAME="toc36"></A>
<H3 CLASS="subsection"><A NAME="htoc68">4.10.1</A>&nbsp;&nbsp;Overview</H3>
The ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> module system controls the visibility of
predicate names,
syntax settings (structures, operators, options, macros),
and non-logical store names (records, global variables).
Predicates and syntax items can be declared local or
they can be exported and imported.
Store names are always local.<BR>
<BR>
<A NAME="toc37"></A>
<H3 CLASS="subsection"><A NAME="htoc69">4.10.2</A>&nbsp;&nbsp;Making a Module</H3>
A source file can be turned into a module by starting it with a 
module directive. A simple module is:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- module(greeting).
:- export hello/0.

hello :-
        who(X),
        printf("Hello %w!%n", [X]).

who(world).
who(friend).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
This is a module which contains two predicates. One of them, hello/0
is exported and can be used by other modules. The other, who/1 is
local and not accessible outside the module.<BR>
<BR>
<A NAME="toc38"></A>
<H3 CLASS="subsection"><A NAME="htoc70">4.10.3</A>&nbsp;&nbsp;Using a Module</H3>
There are 3 ways to use hello/0 from another module.
The first possibility is to import the whole &#8221;greeting&#8221; module.
This makes everything available that is exported from &#8221;greeting&#8221;:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- module(main).
:- import greeting.

main :-
        hello.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
The second possibility is to selectively only import the hello/0
predicate:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- module(main).
:- import hello/0 from greeting.

main :-
        hello.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
The third way is not to import, but to module-qualify the call to hello/0:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- module(main).

main :-
        greeting:hello.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<A NAME="toc39"></A>
<H3 CLASS="subsection"><A NAME="htoc71">4.10.4</A>&nbsp;&nbsp;Qualified Goals</H3>
The module-qualification using <CODE>:/2</CODE> is also used to resolve
name conflicts,
i.e. in the case where a predicate of the same name is defined
in more than one imported module.
In this case, none of the conflicting
predicates is imported - an attempt to call the unqualified predicate
raises an error.
The solution is to qualify every reference with the module name:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
:- lib(ic).       % exports $&gt;= / 2
:- lib(eplex).    % exports $&gt;= / 2

    ..., ic:(X $&gt;= Y), ...
    ..., eplex:(X $&gt;= Y), ...
</PRE></BLOCKQUOTE>
A more unusual feature, which is however very appropriate for
constraint programming, is the possibility to call several versions
of the same predicate by specifying several lookup modules:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
    ..., [ic,eplex]:(X $&gt;= Y), ...
</PRE></BLOCKQUOTE>
which has exactly the same meaning as
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
    ..., ic:(X $&gt;= Y), eplex:(X $&gt;= Y), ...
</PRE></BLOCKQUOTE>
Note that the modules do not have to be known at compile time, i.e. it
is allowed to write code like
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
    after(X, Y, Solver) :-
        Solver:(X $&gt;= Y).
</PRE></BLOCKQUOTE>
This is however likely to be less efficient because it prevents
compile-time optimizations.<BR>
<BR>
<A NAME="toc40"></A>
<H3 CLASS="subsection"><A NAME="htoc72">4.10.5</A>&nbsp;&nbsp;Exporting items other than Predicates</H3>
The most commonly exported items, apart from predicates,
are structure and operator declarations.
This is done as follows:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
:- module(data).
:- export struct(employee(name,age,salary)).
:- export op(500, xfx, reports_to).
...
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
Such declarations can only be imported by importing the whole
module which exports them, i.e. using <TT>import data.</TT>.
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&#8857;</B><DD CLASS="dd-description"> <FONT COLOR="#9832CC">For more details see the User Manual chapter on Modules.</FONT>
</DL>

<HR>
<A HREF="tutorial032.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial034.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
